package com.open.party.cloud.view.home.dangWuMiShu.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.TouPiaoGuanBean;
import com.sinothk.android.utils.XUtils;

import java.util.ArrayList;
import java.util.List;

import com.sinothk.android.utils.inters.OnSuperListener;

public class TouPiaoGuanLiListAdapter extends RecyclerView.Adapter<TouPiaoGuanLiListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<TouPiaoGuanBean> mData;
    private Context mContext;
    private OnSuperListener<TouPiaoGuanBean> onSuperListener;

    public TouPiaoGuanLiListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<TouPiaoGuanBean> onSuperListener) {
        this.onSuperListener = onSuperListener;
    }

    public void setData(List<TouPiaoGuanBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<TouPiaoGuanBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.tou_piao_guan_li_list_adapter_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final TouPiaoGuanBean itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getTitle()));

        String timeStr = "";
        if (itemData.getUpdateTime() == null) {
            timeStr = "最近";
        } else {
            timeStr = XUtils.date().getDateStrByDate(itemData.getUpdateTime(), "yyyy-MM-dd");
        }
        holder.timeTv.setText(timeStr);
//
//        Picasso.get().load(itemData.getImgUrl())
//                .error(R.drawable.default_img)
//                .placeholder(R.drawable.default_img)
//                .into(holder.imageView);

        if (onSuperListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "rootView");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView titleTv, timeTv;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            timeTv = itemView.findViewById(R.id.timeTv);
        }
    }
}